package com.pb.wkflow.api.controller.assembler;

import com.pb.wkflow.api.controller.request.ProcDefineQueryRequest;
import com.pb.wkflow.core.utils.DateUtils;
import com.pb.wkflow.core.utils.ObjectUtils;
import com.pb.wkflow.core.utils.StringUtils;
import org.camunda.bpm.engine.repository.ProcessDefinitionQuery;

/**
 * 流程定义 ProcDefAssembler
 * @author 苗益辉
 * @date 2022/9/1 15:19
 */
public class ProcDefineAssembler {

    private ProcDefineAssembler() {

    }

    /**
     * 查询
     * @param processDefinitionQuery 查询类
     * @param request 请求
     */
    public static void assemblerToQuery(ProcessDefinitionQuery processDefinitionQuery,
                                 ProcDefineQueryRequest request) {
        //流程部署id
        if(StringUtils.isNotBlank(request.getDefinitionId())) {
            processDefinitionQuery.processDefinitionId(request.getDefinitionId());
        }
        //租户id
        if(StringUtils.isNotBlank(request.getTenantIds())) {
            processDefinitionQuery.tenantIdIn(request.getTenantIds().split(","));
        }
        // 流程定义名称
        if(StringUtils.isNotBlank(request.getDefinitionName())) {
            processDefinitionQuery.processDefinitionNameLike("%"+request.getDefinitionName()+"%");
        }
        //流程键值
        if(StringUtils.isNotBlank(request.getDefinitionKey())) {
            processDefinitionQuery.processDefinitionKeyLike("%"+request.getDefinitionKey()+"%");
        }
        //部署时间之后
        if(StringUtils.isNotBlank(request.getDeployTime())) {
            processDefinitionQuery.deployedAfter(DateUtils.strToDate(request.getDeployTime()));
        }
        //版本
        if(ObjectUtils.isNotNull(request.getVersion())) {
            processDefinitionQuery.processDefinitionVersion(request.getVersion());
        }else{
            processDefinitionQuery.latestVersion();
        }
        if(StringUtils.isNotBlank(request.getClassifyId())) {
            processDefinitionQuery.processDefinitionCategory(request.getClassifyId());
        }
        processDefinitionQuery.orderByDeploymentTime();
        processDefinitionQuery.desc();
    }
}
